package com.xqb.modules.controller.course;

import com.xqb.common.annotation.Anonymous;
import com.xqb.common.annotation.Log;
import com.xqb.common.core.controller.BaseController;
import com.xqb.common.core.domain.AjaxResult;
import com.xqb.common.core.page.TableDataInfo;
import com.xqb.common.enums.BusinessType;
import com.xqb.common.utils.poi.ExcelUtil;
import com.xqb.modules.domain.po.LesCourseEvaluation;
import com.xqb.modules.domain.vo.CourseEvaluationVo;
import com.xqb.modules.service.ILesCourseEvaluationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 课程评论Controller
 *
 * @author ruoyi
 * @date 2024-10-16
 */
@RestController
@RequestMapping("/course/evaluation")
@Slf4j
public class LesCourseEvaluationController extends BaseController
{
    @Autowired
    private ILesCourseEvaluationService lesCourseEvaluationService;

    /**
     * 查询课程评论列表
     */
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(LesCourseEvaluation lesCourseEvaluation){
        startPage();
        startOrderBy();
        List<CourseEvaluationVo> list = lesCourseEvaluationService.getLesCourseEvaluationList(lesCourseEvaluation);
        log.info("list:{}", list);
        return getDataTable(list);
    }

    /**
     * 导出课程评论列表
     */
    @PreAuthorize("@ss.hasPermi('course:evaluation:export')")
    @Log(title = "课程评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LesCourseEvaluation lesCourseEvaluation)
    {
        List<LesCourseEvaluation> list = lesCourseEvaluationService.selectLesCourseEvaluationList(lesCourseEvaluation);
        ExcelUtil<LesCourseEvaluation> util = new ExcelUtil<LesCourseEvaluation>(LesCourseEvaluation.class);
        util.exportExcel(response, list, "课程评论数据");
    }

    /**
     * 获取课程评论详细信息
     */
    @PreAuthorize("@ss.hasPermi('course:evaluation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(lesCourseEvaluationService.selectLesCourseEvaluationById(id));
    }

    /**
     * 新增课程评论
     */
    @PreAuthorize("@ss.hasPermi('course:evaluation:add')")
    @Log(title = "课程评论", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LesCourseEvaluation lesCourseEvaluation)
    {
        return toAjax(lesCourseEvaluationService.insertLesCourseEvaluation(lesCourseEvaluation));
    }

    /**
     * 修改课程评论
     */
    @PreAuthorize("@ss.hasPermi('course:evaluation:edit')")
    @Log(title = "课程评论", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LesCourseEvaluation lesCourseEvaluation)
    {
        return toAjax(lesCourseEvaluationService.updateLesCourseEvaluation(lesCourseEvaluation));
    }

    /**
     * 删除课程评论
     */
    @PreAuthorize("@ss.hasPermi('course:evaluation:remove')")
    @Log(title = "课程评论", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(lesCourseEvaluationService.deleteLesCourseEvaluationByIds(ids));
    }

    /**
     * 更新课程评论的点赞数
     *
     * @param id 需要更新点赞数的课程评论ID
     * @return AjaxResult 返回操作结果
     */
    @Log(title = "课程评论", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/likes")
    @PreAuthorize("@ss.hasRole('student')")
    public AjaxResult likes(@RequestBody Long id) {
        return success(lesCourseEvaluationService.updateLikes(id));
    }
}
